1283C - Friends and Gifts - CodeForces Solution


constructive algorithms data structures math *1500

Please click on ads to support us..

Python Code:


q = int(input())
w = list(map(int, input().split()))
e = [0] * q
r = []
t = []
for i in range(q):
    if w[i]==0:
                r.append(i)
    else:
                e[w[i]-1]=1
for i in range(q):
    if e[i]==0:
                t.append(i)
for i in range(len(r)):
    if r[i] == t[i]:
                if i == 0:
                        t[i], t[1] = t[1], t[i]
        else:
                        t[i], t[0] = t[0], t[i]
for i in range(len(r)):
    w[r[i]] = t[i] + 1
print(*w)
    

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define AM17 ios_base::sync_with_stdio(false),cin.tie(NULL);
#define yes cout<<"YES\n";
#define no cout<<"NO\n";

#define lenSorts(s) sort(s.begin(), s.end(), [&] (const string &s, const string &t) { return s.size() < t.size();});
int dx4[4] = { -1, 0, 1, 0 };
int dy4[4] = { 0, 1, 0, -1 };
int dx8[] = {+0, +0, -1, +1, +1, +1, -1, -1};
int dy8[] = {-1, +1, +0, +0, +1, -1, +1, -1};
string Dir = "URDL";
template <class T>
istream & operator>> (istream&is , vector<T> &v )
{
    for (auto &i:v)
        is>> i ;
    return is ;
}
template <class T>
ostream & operator<< (ostream&os ,const vector<T> &v )
{
    for (auto &i:v)
        os << i << " " ;
    os << '\n' ;
    return os ;
}
ll MOD;
ll fastpow(ll a,ll p)
{
    if(p==0)
        return 1ll;

    if(p&1)
        return ((a%MOD) * fastpow(a,p-1))%MOD;
    else
    {
        ll ret=(fastpow(a,p/2));
        return ((ret%MOD) * (ret%MOD))%MOD;
    }
}
//
////ll w[N];
const int N=2e5+10;

//int parent[N];
//struct DSU{
////    vector<ll>cost;
////    ll mx=0;
//    DSU(int n)
//    {
////        sz.resize(N);
//        for (int i = 0; i <= n ; ++i) {
//            parent[i]=i;
////            sz[i]=1;
//        }
//    }
//    int find(int node)
//    {
//        return parent[node];
////        if(parent[node]==node)
////            return parent[node];
////        return parent[node]= find(parent[node]);
//    }
//    bool add(int a,int b)
//    {
////        a= find(a);
////        b= find(b);
////        if(a==b)
////            return false;
////        if(sz[a]<sz[b])
////        { swap(a,b);}
////        parent[b]=a;
////        sz[a]+=sz[b];
//        parent[a]=parent[b];
//        return true;
//    }
//};
//
int main()
{
    int loop=1;
//    cin>>loop;
    while (loop--)
    {
        int n;
        cin>>n;
        vector<pair<int,int>>v(n+1);
        vector<int>send,rec;
        vector<int>ans(n+1);
        for (int i = 1; i <= n; ++i) {
            int f;
            cin>>f;
            ans[i]=f;
            if(f)
            {
                v[i].first=f;
                v[f].second=i;
            }
            else
            {
                send.push_back(i);
            }
        }
        for (int i = 1; i <=n ; ++i) {
            if(!v[i].second)
                rec.push_back(i);
        }
        for (int i = 0; i < send.size(); ++i) {
            if(send[i]==rec[i])
            {
                swap(rec[i],rec[(i+1)%(send.size())]);
            }
        }
        int l=0;
        for (int i = 1; i <=n ; ++i) {
            cout<<(ans[i]?ans[i]:rec[l++])<<" ";
        }



    }
}


Comments

Submit
0 Comments
More Questions

349B - Color the Fence
144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles